/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import MaterialPack.InfoBar;
import Supporter.GlobalVariables;
import Supporter.ImageSupporter;
import Supporter.SoundSupporter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Mantero
 */
public class ResultCanvas extends AbstractCanvas {

    Image BufferImg = null;
    int staminaRemain;
    byte time = 3;
    int v = 870;
    public Image nextscene;
    public static int TotalInsect = 0;
    public int ExpGain = 0;
    public int MoneyGet = 0;
    public int Score = 0;
    public long CurrentExp = 0;
    public int CurrentLV = 0;
    public long lastHighScore = 5;
    long tempExp = 0;
    long tempExpGain = 0;
    long tempExpLvlUp = 0;
    int tempCurrentLV;
    long ExpLvlUp =1000;
    int count = 0;
    long total = 50;
    int factor = 0;
    int temp = 0;  // ve cai hinh chu nhat mau do
    float fact = 0;
    int step = 0;
    boolean wanttoskip = false;
    int FinishcountInsectstatus = -1; // -1 chưa chạy, 0 đang chạy, 1 kết thúc
    int FinishcountExpGainstatus = -1;
    int FinishcountExpstatus = -1;
    int FinishcountMoneystatus = -1;
    int FinishcountScorestatus = -1;
    private Timer managetimer;
    private Timer painter;
    InfoBar EXPbar = null;
    int initvalue;
    private ImageSupporter imgSupporter;
    private Player  ss;

    public ResultCanvas() throws IOException {
        this.setFullScreenMode(true);
        ExpGain = TotalInsect*10;
        MoneyGet = ExpGain;
        Score = MoneyGet+ExpGain;
        
        imgSupporter = new ImageSupporter();
        
        background = imgSupporter.getImage("/result-background.jpg");
        
        nextscene = imgSupporter.getImage("/button-tick.png");
    }

    protected void pointerPressed(int x, int y) {
        if (x > 192 && y > 272) {
            try {
                if (GlobalVariables.sound) {
                ss.stop();
                ss.setLoopCount(1);
                ss.start();
                }
            } catch (MediaException ex) {
                ex.printStackTrace();
            }
            nextscene = imgSupporter.getImage("/button-tick-press.png");
            repaint();
        }
    }

    protected void pointerReleased(int x, int y) {
        nextscene = imgSupporter.getImage("/button-tick.png");


        if (x > 192 && y > 272) {

                if (wanttoskip)
                    try {
                main.toSaveScene();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
           repaint();
           wanttoskip = true;     
        }
        
    }

    public void paint(Graphics g) {
        g.setColor(47, 157, 233);
        g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);   
        if (!wanttoskip) {
            if (FinishcountInsectstatus == 0) {
                g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);    
                g.drawString("Total Insect:" + step, 20, 20, Graphics.LEFT | Graphics.TOP);
            } else if (FinishcountInsectstatus == 1 && FinishcountExpGainstatus == 0) {
                g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                g.drawString("Total Insect:" + TotalInsect, 20, 20, Graphics.LEFT | Graphics.TOP);
                g.drawString("Exp Gain:" + step, 20, 40, Graphics.LEFT | Graphics.TOP);
            } else if (FinishcountExpGainstatus == 1 && FinishcountMoneystatus == 0) {
                g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                g.drawString("Total Insect:" + TotalInsect, 20, 20, Graphics.LEFT | Graphics.TOP);
                g.drawString("Exp Gain:" + ExpGain, 20, 40, Graphics.LEFT | Graphics.TOP);
                g.drawString("Money Bonus:" + step, 20, 60, Graphics.LEFT | Graphics.TOP);
            } else if (FinishcountMoneystatus == 1 && FinishcountScorestatus == 0) {
                g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                g.drawString("Total Insect:" + TotalInsect, 20, 20, Graphics.LEFT | Graphics.TOP);
                g.drawString("Exp Gain:" + ExpGain, 20, 40, Graphics.LEFT | Graphics.TOP);
                g.drawString("Money Bonus:" + MoneyGet, 20, 60, Graphics.LEFT | Graphics.TOP);
                g.drawString("Your Score:" + step, 20, 80, Graphics.LEFT | Graphics.TOP);
            } else if (FinishcountScorestatus == 1 && FinishcountExpstatus == 0) {
                g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
                g.drawString("Total Insect:" + TotalInsect, 20, 20, Graphics.LEFT | Graphics.TOP);
                g.drawString("Exp Gain:" + ExpGain, 20, 40, Graphics.LEFT | Graphics.TOP);
                g.drawString("Money Bonus:" + MoneyGet, 20, 60, Graphics.LEFT | Graphics.TOP);
                g.drawString("Your Score:" + Score, 20, 80, Graphics.LEFT | Graphics.TOP);

                int ratio = (int) ((step + tempExp) * 100 / tempExpLvlUp);
                System.out.println("ratio in A: " +ratio+" "+step+" "+tempExp+" "+tempExpLvlUp);
                EXPbar.setRatio(ratio);
                EXPbar.paint(g);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
                g.drawString("LV:" + tempCurrentLV, 20, 115, Graphics.LEFT | Graphics.TOP);
                g.setFont(Font.getDefaultFont());
            }

        } else {
            g.drawImage(background, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.drawString("Total Insect:" + TotalInsect, 20, 20, Graphics.LEFT | Graphics.TOP);
            g.drawString("Exp Gain:" + ExpGain, 20, 40, Graphics.LEFT | Graphics.TOP);
            g.drawString("Money Bonus:" + MoneyGet, 20, 60, Graphics.LEFT | Graphics.TOP);
            g.drawString("Your Score:" + Score, 20, 80, Graphics.LEFT | Graphics.TOP);
            int ratio = 0;
            CurrentLV += (CurrentExp + temp) / ExpLvlUp;
            CurrentExp = (CurrentExp + temp) % ExpLvlUp;
            if ((CurrentExp + temp) / ExpLvlUp >0)
                ExpLvlUp = ExpLvlUp+ CurrentLV*100;
            temp = 0;
            
            System.out.println("ratio in B: " +ratio+" "+CurrentExp+" "+ExpLvlUp+" "+temp);
            ratio = (int) ((CurrentExp) * 100 / ExpLvlUp);
            
            main.getPet().SetAttributes(0, (int) CurrentExp);
            main.getPet().SetAttributes(1, CurrentLV);
            EXPbar.setRatio(ratio);
            EXPbar.paint(g);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            g.drawString("LV:" + CurrentLV, 20, 115, Graphics.LEFT | Graphics.TOP);
            g.setFont(Font.getDefaultFont());
        }

        g.drawImage(nextscene, getWidth(), 320, Graphics.RIGHT | Graphics.BOTTOM);
    }

    public void createCanvas(MainMIDlet midlet,Player player) {
        main = midlet;
        ss = player;
        CurrentLV = main.getPet().getLevel();
         
        ExpLvlUp= CurrentLV*1000;
         
        CurrentExp = main.getPet().GetAttributes(0);
        CurrentLV = main.getPet().GetAttributes(1);
        System.out.println("currentEXP: "+CurrentExp);
        System.out.println("currentLV: "+CurrentLV);
        tempExp = CurrentExp;
        tempExpGain = ExpGain;
        temp = ExpGain;
        tempExpLvlUp = ExpLvlUp;
        tempCurrentLV = CurrentLV;
        EXPbar = new InfoBar("/option-expbar.png", "/option-expbar-mask.png", InfoBar.HORIZON, (int) (CurrentExp * 100 / ExpLvlUp), 20, 135, 127, 14);
        TimerTask ManageTask = new TimerTask() {

            public void run() {
                if (!wanttoskip) {
                    if (FinishcountInsectstatus == -1) {
                        FinishcountInsectstatus = 0;
                        painter = new Timer();
                        painter.schedule(new TimerTask() {

                            public void run() {
                                if (step < TotalInsect) {
                                    step++;
                                } else {
                                    step = 0;
                                    FinishcountInsectstatus = 1;
                                    this.cancel();

                                }
                                repaint();


                            }
                        }, 0, 50);
                    } else if (FinishcountExpGainstatus == -1 && FinishcountInsectstatus == 1) {
                        step = 0;
                        FinishcountExpGainstatus = 0;
                        painter.schedule(new TimerTask() {

                            public void run() {
                                if (step < ExpGain) {
                                    step++;

                                } else {
                                    step = 0;
                                    FinishcountExpGainstatus = 1;
                                    this.cancel();

                                }
                                repaint();


                            }
                        }, 0, 1);
                    } else if (FinishcountMoneystatus == -1 && FinishcountExpGainstatus == 1) {
                        step = 0;
                        FinishcountMoneystatus = 0;
                        painter.schedule(new TimerTask() {

                            public void run() {
                                if (step < MoneyGet) {
                                    step++;
                                } else {
                                    step = 0;
                                    FinishcountMoneystatus = 1;
                                    this.cancel();

                                }
                                repaint();


                            }
                        }, 0, 1);
                    } else if (FinishcountScorestatus == -1 && FinishcountMoneystatus == 1) {
                        step = 0;
                        FinishcountScorestatus = 0;
                        painter.schedule(new TimerTask() {

                            public void run() {
                                if (step < Score) {
                                    step++;
                                } else {
                                    step = 0;
                                    FinishcountScorestatus = 1;
                                    this.cancel();

                                }
                                repaint();


                            }
                        }, 0, 1);
                    } else if (FinishcountExpstatus == -1 && FinishcountScorestatus == 1) {
                        step = 0;
                        FinishcountExpstatus = 0;
                        painter.schedule(new TimerTask() {

                            public void run() {
                                if (step < tempExpGain && tempExp + step < tempExpLvlUp) {
                                    step++;
                                } else if (step + tempExp >= tempExpLvlUp) {
                                    tempCurrentLV++;
                                    main.getPet().SetAttributes(1, tempCurrentLV);
                                    //CurrentExp += step;
                                    step = 0;
                                    tempExpGain = (int) ((tempExpGain + tempExp) - tempExpLvlUp);
                                    tempExp = 0;
                                    tempExpLvlUp *= 3;
                                    // lvl ++
                                } else {
                                    //CurrentExp += step;
                                    tempExp = step;
                                    FinishcountExpstatus = 1;
                                    wanttoskip=true;
                                    this.cancel();
                                  //  System.out.print("CurrentExp" + CurrentExp);
                                  //  System.out.print("LvlUp" + tempExpLvlUp);
                                }
                                repaint();

                            }
                        }, 0, 1);
                    }
                } else {
                    main.getPet().SetAttributes(0, (int)CurrentExp);
                    main.getPet().SetAttributes(1, (int)CurrentLV);
                    main.getPet().SetAttributes(5, (int)MoneyGet);
                    main.getPet().setScore(Score);
                    this.cancel();
                }
            }
        };
        // Start painting
        managetimer = new Timer();
        managetimer.schedule(ManageTask, 1000, 50);
        
    }
}
